<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Uji_lab extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
        $this->load->library('form_validation');	
		$this->load->model('model_uji_lab');		
	}
	
	function index() {		
		$colModel['Edit'] = array('Edit', 75, TRUE, 'center', 0);
		$colModel['id_uji'] = array('ID Pengujian', 200, TRUE, 'center', 0);
        $colModel['tgl_uji'] = array('Tanggal Uji', 100, TRUE, 'center', 0);
		$colModel['LAPL'] = array('LAPL', 200, TRUE, 'center', 0);
		$colModel['IPA_LINDI'] = array('IPA LINDI', 200, TRUE, 'center', 1);
        $colModel['IPLT'] = array('IPLT', 200, TRUE, 'center', 1);
		  
        $gridParams = array(
            'width' => 1100,
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Uji laboratorium',
            'showTableToggleBtn' => true
        );

        //$buttons[] = array('Tambah','add','test');
        //$buttons[] = array('separator');

        $buttons[] = array('Tambah', 'add', 'spt_js');
        $buttons[] = array('separator');
        $buttons[] = array('Hapus', 'delete', 'spt_js');
		$buttons[] = array('separator');
        $buttons[] = array('Select All', 'add', 'spt_js');
        $buttons[] = array('Deselect All', 'delete', 'spt_js');
        $buttons[] = array('separator');

        // mengambil data dari file controler ajax pada method grid_berkas
        $grid_js = build_grid_js('flex1', site_url("uji_lab/uji_lab/grid_uji_lab"), $colModel, 'Tanggal_Uji', 'asc', $gridParams, $buttons);


        //additional JS
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/uji_lab/uji_lab/form_uji_lab'; 
			}
			
			if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
			if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data uji laboratiruam?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/uji_lab/uji_lab/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";

        $data['js_grid'] = $grid_js;
        //remdering view
        $data['title'] = 'Daftar Uji Laboratorium';
        $data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
    }
	
	function grid_uji_lab() {
        $valid_fields = array('Edit', 'id_uji', 'Tanggal_Uji','LAPL', 'IPA_LINDI', 'IPLT');
        $this->flexigrid->validate_post('Tanggal_Uji', 'desc', $valid_fields);
        $records = $this->model_uji_lab->get_uji_lab_grid();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            $link_edit = "<a href='" . base_url() . "index.php/uji_lab/uji_lab/edit/" . $row->ID_UJI. "'>";
            $image_edit = "" . base_url() . "images/main/edit.png";
			$img = $link_edit."<img src=".$image_edit." /></a>";
			
			
            $record_items[] = array(
				$row->ID_UJI,
				$img,
				$row->ID_UJI,
				$row->TANGGAL_UJI,
				$row->LAPL,
                $row->IPA_LINDI,
				$row->IPLT
            );
			
			
        }
		

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	
	 function cek_validasi() {
        // Setting Rule
        $this->form_validation->set_rules('tanggal_uji', 'Tanggal Uji Laboratorium', 'required');
		$this->form_validation->set_rules('id_uji', 'ID Uji Laboratorium', 'required|numeric');

        // Setting Error Message
        $this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
		$this->form_validation->set_message('numeric', 'Kolom %s harus diisi angka!!');

        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }
	
	public function form_uji_lab() {
        $data['status'] = 'new';
        $data['title'] = 'Tambah Data Uji Laboratorium';
		
		$data['content'] = $this->load->view('uji_lab/form_uji_lab',$data,true);
		$this->load->view('main',$data);
    }

    public function insert_uji_lab() {
		if ($this->cek_validasi() == TRUE) {

			$data = array(
				'ID_UJI' => $this->input->post('id_uji'),
				'TANGGAL_UJI' => $this->input->post('tanggal_uji'),
				'LAPL' => $this->input->post('LAPL'),
				'IPA_LINDI' => $this->input->post('IPA_LINDI'),
				'IPLT' => $this->input->post('IPLT'),
			);
			
			
			$this->model_uji_lab->insert_uji_lab($data);
			redirect('uji_lab/uji_lab');
			
		}        
                
		else {
			//redirect('uji_lab/uji_lab');
			$this->form_uji_lab();
			}
	}

    public function edit($id) {
        $data['status'] = 'edit';
        $record_uji_lab = $this->model_uji_lab->selectone($id);
        foreach ($record_uji_lab->result() as $uji_lab) {
            $data['id_uji'] = $uji_lab->ID_UJI;
            $data['tanggal_uji'] = $uji_lab->TANGGAL_UJI;
			$data['LAPL'] = $uji_lab->LAPL;
            $data['IPA_LINDI'] = $uji_lab->IPA_LINDI;
			$data['IPLT'] = $uji_lab->IPLT;
        }
		
		$data['status'] = 'edit';
		$data['title'] = 'Edit Data Uji Laboratorium';
		$data['content'] = $this->load->view('uji_lab/form_uji_lab',$data,true);
		$this->load->view('main',$data);
   
    }

    //update peserta
    public function update_uji_lab($id) {
		
			if ($this->cek_validasi() == TRUE) {
			
			$data = array(
				'ID_UJI' => $this->input->post('id_uji'),
				'TANGGAL_UJI' => $this->input->post('tanggal_uji'),
				'LAPL' => $this->input->post('LAPL'),
				'IPA_LINDI' => $this->input->post('IPA_LINDI'),
				'IPLT' => $this->input->post('IPLT'),
			);
			$this->model_uji_lab->update_uji_lab($data, $id);
			redirect('uji_lab/uji_lab');
                    
            } 
			else {
			 $this->edit($id);
			}
		
	}

    public function delete($id) {
        $spt_ids_post_array = split(",", $this->input->post('items'));
        foreach ($spt_ids_post_array as $index => $id) {
            if (isset($id) && $id != '') {
                $this->model_uji_lab->delete_uji_lab($id);
            }//end if
        }//end foreach
    }
}